Repository navigation
unor4wifi: add WiFi driver for the Arduino UNO R4 WiFi - #940
Conversation
dgryski
left a comment
There was a problem hiding this comment.
Thanks for this. I compared the driver against WiFiS3 and the bridge firmware, and ran the AP path on an UNO R4 WiFi (bridge 0.6.0). AP mode is broken (inline comment), and the AT argument escaping needs work before merge. The rest are smaller.
I also saw the ntpclient smoketest build fail to link for arduino-uno-r4-wifi with __atomic_fetch_sub_8 missing, using a local tinygo build. That may be specific to my tree, so please check it against the tinygo version CI uses.
| // Channel 1 is the WiFiS3 default. | ||
| cmd := "AT+BEGINSOFTAP=" + escapeArg(p.Ssid) + "," + escapeArg(p.Passphrase) + ",1" | ||
| n, err := d.modem.execInt(cmd) | ||
| if err != nil || n != wlAPListening { |
There was a problem hiding this comment.
AP mode always fails here. The bridge returns the bool from WiFi.softAP(), so success is 1. WiFiS3 checks == 1 too. wlAPListening (7) is a status value, not the return value.
On hardware the log shows +BEGINSOFTAP: 1 then OK, and the driver still returns ErrConnectFailed. Changing the check to n != 1 gives CONNECTED with IP 192.168.4.1.
| // escapeArg escapes the characters that the bridge argument parser treats | ||
| // as special. See uno-r4-wifi-usb-bridge/UNOR4USBBridge/parser.cpp, | ||
| // ATParser::parse, state Argument. | ||
| func escapeArg(s string) string { |
There was a problem hiding this comment.
This does not escape CR or LF. In the bridge parser (parser.cpp) the \n check runs before the escape handling, so a newline in an SSID or passphrase ends the command early. Please reject control characters in user supplied strings, or return an error.
| d.mu.Lock() | ||
| defer d.mu.Unlock() | ||
|
|
||
| res, err := d.modem.exec("AT+HOSTBYNAME=" + name) |
There was a problem hiding this comment.
name is not escaped here. A comma or quote adds extra arguments. The same applies to host in the AT+CLIENTCONNECT command in Connect. Please run both through escapeArg (plus the control character check).
| return netip.AddrPortFrom(ip, uint16(port)) | ||
| } | ||
|
|
||
| func (d *device) Accept(sockfd int) (int, netip.AddrPort, error) { |
There was a problem hiding this comment.
Two problems in Accept.
- Any error from
SERVERACCEPT(ERROR or a modem timeout) is treated as no connection, so it loops forever with no deadline and no error. - If
newSocketfails after the firmware accepted a client, that firmware socket is never closed and leaks.
Please return real errors and send AT+CLIENTCLOSE on the failure path.
| return nil | ||
| } | ||
|
|
||
| func (d *device) NetDisconnect() { |
There was a problem hiding this comment.
NetDisconnect leaves d.sockets as is, and NetConnect calls reset() (SOFTRESETWIFI), which clears the firmware sockets. After a reconnect the Go table can hold entries whose modemSock no longer exists. Please clear the table on disconnect or reset.
| } | ||
| err = d.connectAP() | ||
| default: | ||
| return netlink.ErrConnectModeNoGood |
There was a problem hiding this comment.
This mode check happens after setupUART and reset. Validating ConnectMode at the top of NetConnect avoids the side effects for a bad mode.
| dns netip.Addr | ||
|
|
||
| sockets [maxSockets]*socket | ||
| rxbuf [maxChunk]byte |
There was a problem hiding this comment.
rxbuf is never used. It costs 1 KB of the 32 KB of RAM.
| } | ||
|
|
||
| // The firmware resolves names and dotted addresses through the same | ||
| // path, so always use +CLIENTCONNECT like the Arduino WiFiS3 library. |
There was a problem hiding this comment.
This comment is not quite right. WiFiS3 only uses +CLIENTCONNECT when a connection timeout is set and otherwise uses +CLIENTCONNECTNAME. The firmware does accept +CLIENTCONNECT with a timeout argument, so the code works. Please fix the comment.
| } | ||
|
|
||
| // readLine reads a line and strips the trailing CR LF. | ||
| func (m *modem) readLine(deadline time.Time) (string, error) { |
There was a problem hiding this comment.
m.line can grow until the deadline if no \n arrives. At 115200 baud and a 10 s timeout that is far more than the RAM available. Please cap the line length.
|
How is this different from https://github.com/tinygo-org/drivers/tree/dev/espat ? |
The Uno R4 WiFi runs a custom firmware on it's esp32 so espat doesn't work on this board. Could probably reflash the esp32 but I believe keeping it as is would be better. |
|
@dgryski Thanks for your review. I have done some changes to cover your comments. |
|
Thanks for the fixes. I re-read the new commit against the bridge firmware source and ran it on an UNO R4 WiFi (bridge 0.6.0, Earlier comments All of them are addressed. AP mode now returns an address (192.168.4.1), control characters are rejected before any modem traffic, DNS names and connect hosts are escaped, Hardware results 20 checks pass: STA connect on a real network, DNS (including a comma in the name and CRLF rejection), TCP GET from example.com, AP start, and UDP socket open, close and reuse. Please remove The driver uses Remaining issues I reproduced each of these on the board. None of them is a crash or data bug, so I'd count them as polish or follow-ups.
Other notes
|
|
New round of changes has been pushed covering your mentioned issues and notes. |
dgryski
left a comment
There was a problem hiding this comment.
LGTM. Can you please rebase and fix the conflict on the smoketest script so this can be merged?
Thanks for working on this! Having wifi support for this popular board is a great addition.
Talks AT commands over UART to the ESP32-S3 bridge firmware and implements netlink and netdev. Supports STA and AP mode, TCP client and server, UDP and DNS. Adds a netlink probe and enables the small net examples for the arduino_uno_r4_wifi build tag.
Add an optional watchdog that reports EventNetDown and EventNetUp and reconnects in station mode. Fail fast with ErrConnectFailed when the SSID is not found. Allow an open AP with an empty passphrase and clear the socket table in NetDisconnect.
|
Done, thank you for your valuable input. |
Talks AT commands over UART to the ESP32-S3 bridge firmware and implements netlink and netdev. Supports STA and AP mode, TCP client and server, UDP and DNS. Adds a netlink probe and enables the small net examples for the arduino_uno_r4_wifi build tag.
Tested by serving http server and misc wifi discovery.